package com.lightsaber.trade.as.service.rollover.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.lightsaber.trade.as.dao.archive.ArchivedImageDao;
import com.lightsaber.trade.as.dao.planning.ProgrammePlanningItemDao;
import com.lightsaber.trade.as.entity.archive.ArchivedImage;
import com.lightsaber.trade.as.entity.planning.Level2Planning;
import com.lightsaber.trade.as.entity.planning.Planning;
import com.lightsaber.trade.as.entity.planning.PlanningStatus;
import com.lightsaber.trade.as.entity.planning.ProgrammePlanningItem;
import com.lightsaber.trade.as.service.planning.Level2PlanningService;
import com.lightsaber.trade.as.service.planning.PlanningService;
import com.lightsaber.trade.as.service.rollover.RolloverImageService;

@Service
@Component
@Transactional
public class RolloverImageServiceImpl implements RolloverImageService {
    @Autowired
    private ArchivedImageDao archivedImageDao;

    @Autowired
    private ProgrammePlanningItemDao programmePlanningItemDao;

    @Autowired
    private PlanningService planningService;

    @Autowired
    private Level2PlanningService level2PlanningService;

    @Override
    public void delete(final ArchivedImage t) {
        // TODO Auto-generated method stub

    }

    @Override
    public void delete(final String id) {
        // TODO Auto-generated method stub

    }

    @Override
    public List<ArchivedImage> getAll() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public ArchivedImage get(final String id) {
        return archivedImageDao.get(id);
    }

    @Override
    public void save(final ArchivedImage t) {
        // TODO Auto-generated method stub

    }

    @Override
    public void update(final ArchivedImage t) {
        // TODO Auto-generated method stub

    }

    @Override
    public Page<ArchivedImage> findPage(final Page<ArchivedImage> page,
            final List<PropertyFilter> filters) {

        return null;
    }

    @Override
    public Page<ArchivedImage> findPage(final ArchivedImage archivedImage,
            final Page<ArchivedImage> page) {
        return archivedImageDao.findPage(archivedImage, page);
    }

    private void copyPlanningLevel2(final String planningIdSources, final String planningIdTarget,
            final String remarks, final boolean isUseForLevel3,
            final Map<String, String> PlanningItemOldMapNew) {
        Level2Planning level2PlanningSources = level2PlanningService
                .findByPlanningId(planningIdSources);
        // save level2Planning
        Level2Planning level2PlanningTarget = new Level2Planning();
        level2PlanningTarget.setPlanningExercise(level2PlanningSources.getPlanningExercise() + 1);
        level2PlanningTarget.setRemarks(remarks);
        level2PlanningTarget.setPlanning(planningService.get(planningIdTarget));
        level2PlanningService.save(level2PlanningTarget);
        List<ProgrammePlanningItem> items = level2PlanningSources.getProgrammePlanningItems();
        for (ProgrammePlanningItem item : items) {
            ProgrammePlanningItem newItem = new ProgrammePlanningItem();
            newItem.setLevel2Planning(level2PlanningTarget);
            newItem.setCode(item.getCode());
            newItem.setOperatingCampus(item.getOperatingCampus());
            newItem.setOperatingUnit(item.getOperatingUnit());
            newItem.setPlannedPlacesNew(item.getPlannedPlacesNew());
            newItem.setPlannedPlacesProg(item.getPlannedPlacesProg());
            newItem.setPlannedPlacesNewActual(item.getPlannedPlacesNewActual());
            newItem.setPlannedPlacesProgActual(item.getPlannedPlacesProgActual());
            newItem.setPlanYear(item.getPlanYear() + 1);
            newItem.setStudyYear(item.getStudyYear() + 1);
            programmePlanningItemDao.save(newItem);
            if (isUseForLevel3) {
                PlanningItemOldMapNew.put(item.getId(), newItem.getId());
            }
        }
    }

    @Override
    public void rolloverLevel2(final String planningIdSources, final String planningIdTarget,
            final String remarks) {
        Planning planning = planningService.get(planningIdTarget);
        planning.setStatus(PlanningStatus.WaitingForSubmission);
        // update planning
        planningService.update(planning);
        copyPlanningLevel2(planningIdSources, planningIdTarget, remarks, false, null);
    }
}
